package a51qindian.com.comprehensive.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.li.interfaces.OnItemClickListener;
import com.li.widget.DoubleTextView;

import java.util.List;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.fragment.RepairFragment;
import a51qindian.com.comprehensive.model.RepairInfo;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Li
 * Date: 2017/9/22 0022
 * Description: 维系适配器
 */

public class RepairAdapter extends RecyclerView.Adapter<RepairAdapter.RepairHolder> {



    private LayoutInflater mInflater;
    private String mType;
    private List<RepairInfo> mRepairInfoList;

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    private OnItemClickListener mItemClickListener;

    public RepairAdapter(Context context, String type) {
        mInflater = LayoutInflater.from(context);
        mType = type;
    }

    @Override
    public RepairHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_repair, parent, false);

        final RepairHolder holder = new RepairHolder(view);
        switch (mType) {
            case RepairFragment.REPAIR_UN_FINISHED:
                holder.mTxtRepairInfo.setVisibility(View.GONE);
                if (mItemClickListener != null) {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mItemClickListener.onItemClick(v,holder.getLayoutPosition());
                        }
                    });
                }
                break;
            case RepairFragment.REPAIR_FINISHED:
                holder.mTxtRepairInfo.setVisibility(View.VISIBLE);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RepairHolder holder, int position) {
        RepairInfo repairInfo=mRepairInfoList.get(position);
        holder.mTxtShopName.setTxtRight(repairInfo.getName());
        holder.mTxtContact.setTxtRight(repairInfo.getLinkman());
        holder.mTxtPhone.setTxtRight(repairInfo.getMobile());
        holder.mTxtAddress.setTxtRight(repairInfo.getAddress());
        holder.mTxtRepairInfo.setVisibility(View.GONE);
        holder.mTxtSn.setTxtRight(repairInfo.getSn());
    }

    @Override
    public int getItemCount() {
        return mRepairInfoList == null ? 0 : mRepairInfoList.size();
    }

    public void setRepairInfoList(List<RepairInfo> repairInfoList) {
        mRepairInfoList = repairInfoList;
        notifyDataSetChanged();
    }

    static class RepairHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_shop_name)
        DoubleTextView mTxtShopName;
        @BindView(R.id.txt_contact)
        DoubleTextView mTxtContact;
        @BindView(R.id.txt_phone)
        DoubleTextView mTxtPhone;
        @BindView(R.id.txt_address)
        DoubleTextView mTxtAddress;
        @BindView(R.id.txt_sn)
        DoubleTextView mTxtSn;
        @BindView(R.id.txt_repair_info)
        DoubleTextView mTxtRepairInfo;
        public RepairHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
